package com.aop;

import com.util.SessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
 * 切面类
 */
@Component
@Aspect
public class TransectionAspect {

    @Before("execution(* com.service.*.*(..))")
    public void before(JoinPoint point){
        System.out.println("我在所有方法之前执行");
    }


    public Object go(ProceedingJoinPoint jp,Transaction transaction){
        Object obj=null;
        SqlSession session= SessionUtil.getSession();
        try{
            obj=jp.proceed(); //调用原来业务层的方法
            session.commit();
        }catch (Throwable e){
            session.rollback();
        }
        finally {
            session.close();
        }
        return obj;
    }
}
